
import { _decorator, Component, Node, Enum, v3, Tween, tween, Widget, easing } from 'cc';
const { ccclass, property } = _decorator;
enum StayAnimType {
    None,
    LoopScale,  //循环缩放
}
//缓动动画
enum EasingType {
    SineIn,
    SineOut,
    SineInOut,
    BackIn,
    BackOut,
    BackInOut,
}
//持续显示的动画+可以和UIAnimation配合
@ccclass('UIStayAnimations')
export class UIStayAnimations extends Component {
    //是否是自主控制播放动画
    _isAutoShow = false;

    //--缩放属性--
    @property({ type: Enum(StayAnimType) })
    protected animType: StayAnimType = StayAnimType.None;
    @property({ displayName: '单循环时间', visible() { return this.animType == StayAnimType.LoopScale } })
    protected scaleTime = 1;
    @property({ displayName: '缩放大小', visible() { return this.animType == StayAnimType.LoopScale } })
    protected scaleRate = 0.2;
    @property({ displayName: '使用缩放缓动动画', visible() { return this.animType == StayAnimType.LoopScale } })
    protected useScaleAnim = false;
    @property({ type: Enum(EasingType), displayName: '缩小动画类型', visible() { return this.animType == StayAnimType.LoopScale && this.useScaleAnim } })
    protected scaleInType = EasingType.SineIn;
    @property({ type: Enum(EasingType), displayName: '放大动画类型', visible() { return this.animType == StayAnimType.LoopScale && this.useScaleAnim } })
    protected scaleOutType = EasingType.SineIn;

    _cfg = ['sineIn', 'sineOut', 'sineInOut', 'backIn', 'backOut', 'backInOut'];

    //---动画属性---
    protected _initPos = v3();
    protected _initScale = v3();
    protected _initAng = v3();
    protected _pos = v3();
    protected _scale = v3();
    protected _ang = v3();

    //---动态属性
    private _isScaleAnim = false;

    onLoad() {
        //如果当前节点同时挂载 UIAnimations 和 UIStayAnimations ,则在入场动画结束之后执行此脚本
        let _uianim = this.node.getComponent('UIAnimtions');
        this._isAutoShow = false;
        if (_uianim == undefined || _uianim && !_uianim.enabled) {
            this._isAutoShow = true;
        }

        this._initPos.set(this.node.position);
        this._initScale.set(this.node.scale);
        this._initAng.set(this.node.eulerAngles);
    }

    onEnable() {
        this.resetAnim();

        if (this._isAutoShow) {
            this.showAnim();
        }
    }
    //重置状态
    resetAnim() {
        Tween.stopAllByTarget(this._pos);
        Tween.stopAllByTarget(this._scale);
        Tween.stopAllByTarget(this._ang);
        this._isScaleAnim = false;
    }

    showAnim() {
        if (this.animType == StayAnimType.None) return;

        //取消widget
        let wg = this.node.getComponent(Widget);
        if (wg) {
            wg.enabled = false;
        }

        switch (this.animType) {
            case StayAnimType.LoopScale:
                this.loopScaleStepAnim();
                break;

            default:
                break;
        }
    }

    //#region --------循环缩放动画----------------------------    
    //循环动画 的 过渡动画:scale 变为1
    loopScaleStepAnim() {
        Tween.stopAllByTarget(this._scale);
        this._scale.set(this.node.scale);
        //过渡动画时间
        let time0 = Math.abs(this.node.scale.x - 1) * this.scaleTime;
        if (time0 >= 0.03) {
            //执行过渡动画
            tween(this._scale).to(time0, { x: 1, y: 1, z: 1 }).call(() => {
                this.loopScaleAnim();
            }).start();
        } else {
            //跳过过渡动画            
            this.loopScaleAnim();
        }
    }

    //循环缩放动画:-默认从 scale=1 开始
    loopScaleAnim() {
        Tween.stopAllByTarget(this._scale);
        this._isScaleAnim = true;
        //缩放动画
        let ms = 1 - this.scaleRate;
        if (this.useScaleAnim) {
            //使用缓动
            let estypeIn: any = this._cfg[this.scaleInType];
            let estypeOut: any = this._cfg[this.scaleInType];
            tween(this._scale).repeatForever(
                tween(this._scale).
                    to(this.scaleTime / 2, { x: ms, y: ms, z: ms }, { easing: estypeIn }).
                    to(this.scaleTime / 2, { x: 1, y: 1, z: 1 }, { easing: estypeOut }).
                    start()
            ).start();
        } else {
            //不使用缓动
            tween(this._scale).repeatForever(
                tween(this._scale).
                    to(this.scaleTime / 2, { x: ms, y: ms, z: ms }).
                    to(this.scaleTime / 2, { x: 1, y: 1, z: 1 }).
                    start()
            ).start();
        }
    }
    //#endregion

    update(dt) {
        //更新缩放
        if (this._isScaleAnim && this.node) {
            this.node.setScale(this._scale);
        }
    }



}
